var contactSaveSubject = {
	saveSubject : function() {
		var contactSubject = $F('contactSubject');
		
		if ( (contactSubject == '') && ($F('contactLogLoaded') != 1) ) {
			alert('You must enter a subject for this contact.');
			$('contactSubject').focus();
		} else {		
			var contactID = $F('contactID');
			if (contactSubject != '') {
				var params = 'handler=saveSubject&subjectString='+contactSubject+'&contactID='+contactID;
				var ajaxResults = new Ajax.Request('ajaxDirector.php', {
					method: 'POST',
					parameters: params
				});
			}	
		}	
	}
}

var contactSaveReferralInfoObj = {
	toggleReferralItem : function(referralItem) {
		var referralColumn = 'Referral'+referralItem;
		var contactID = $F('contactID');
		var params = 'handler=toggleReferralItem&referralColumn='+referralColumn+'&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params
		});
		
	},
	saveCareNotebook : function() {
		var selectedLanguageString = '';
		var allLanguageCheckboxes  = document.getElementsByClassName('checkboxCareNotebookLanguage');
		for(i=0;i<allLanguageCheckboxes.length;i++) {
			if (allLanguageCheckboxes[i].checked == true) {
				selectedLanguageString += allLanguageCheckboxes[i].value+'|';					
			} else {
				selectedLanguageString += '0|';
			}
		}
		var careNotebookNumber = $F('careNotebookNumber');
		var contactID = $F('contactID');
		var params = 'handler=saveCareNotebookItems&contactID='+contactID+'&langString='+selectedLanguageString+'&number='+careNotebookNumber;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				updateStatus('Items updated!','success','messageCareNotebook');
			}
		});		
	},
	saveFollowUpRequired : function() {
		var contactID = $F('contactID');
		
		/*
		 * Turn on activity indicator
		 */
		updateStatus('Sending follow-up email.','wait','messageFollowUp');
				
		// Get selected staff members for follow-up.
		var selectedStaffString = '';
		var allStaffCheckboxes  = document.getElementsByClassName('followUpStaffCheckbox');
		for(i=0;i<allStaffCheckboxes.length;i++) {
			if (allStaffCheckboxes[i].checked == true) {
				selectedStaffString += allStaffCheckboxes[i].value+'|';					
			}			
		}
		
		var followUpNotes = $F('followUpNotes');
		var followUpEmail = $F('followUpAdditionalRecipients');
		
		if (followUpNotes == 'Notes or Instructions:') {
			followUpNotes = '';
		}
		if (followUpEmail == 'Additional recipients: separate multiple email addresses with commas') {
			followUpEmail = '';
		}		
		
		var params = 'handler=saveFollowUpItems&contactID='+contactID+'&selectedStaffString='+selectedStaffString+'&followUpNotes='+followUpNotes+'&followUpEmail='+followUpEmail;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				updateStatus('Items updated!','success','messageFollowUp');
				contactSaveReferralInfoObj.returnSaveFollowUpRequired(transport.responseText);
			},
			onFailure: function() {
				alert('Oops!');
			}		
		});		
	},
	returnSaveFollowUpRequired : function(response) {
		responseArray = response.split("|");
		var error = responseArray[0];
		var errorMessage = responseArray[1];
		
		if (error == 1) {
			// Render error message
			$('errorMessageFollowUp').className = 'errorMessageSmall';
			$('errorMessageFollowUp').innerHTML = 'There are problems with the following email addresses: <b>'+errorMessage+'</b>';
		} else {
			// Close Follow-up Required panel.
			contactFormSupportObj.hideReferralPanel('panelFollowUpItems');
		}		
	},
	saveEvents : function() {
		var contactID = $F('contactID');
		var referralEvents = document.getElementsByClassName('referralEvents');
		var eventIDString = '';
		
		for(i = 0; i < referralEvents.length; i++) {
			/*
			 * Log any checked events to database
			 */
			if (referralEvents[i].checked == true) {
				var eventID = referralEvents[i].value;
				eventIDString += eventID+'|';				
			}
		}		
		var params = 'handler=saveReferralEvent&eventID=&eventIDString='+eventIDString+'&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params
		});
		updateStatus('Event referrals updated!','success','messageEvents');
	},
	saveLibraryItems : function(libraryID) {
		var contactID = $F('contactID');
		var params = 'handler=saveReferralLibraryItem&libraryID='+libraryID+'&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				contactFormGetInfoObj.renderLibraryReferralItems(transport.responseText);
			},
			onFailure: function() {
				alert('Oops!');
			}	
		});
	},
	saveOtherOrgReferral : function() {
		var contactID = $F('contactID');
		var params = 'handler=saveReferralOtherOrg&libraryID='+libraryID+'&contactID='+contactID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,
			onSuccess: function(transport) {
				contactFormGetInfoObj.renderLibraryReferralItems(transport.responseText);
			},
			onFailure: function() {
				alert('Oops!');
			}	
		});
	}
}

var contactSaveFamilyTeamObj = {
	saveTeamMember : function(tabID) {
		var contactID = $F('contactID');
		var error = 0;
		
		// Check for required fields
		if ( ($F('nameFirst'+tabID) == '') && ($F('phone1'+tabID) == '') ) {
			alert('You must at least enter a First Name or Phone Number.');
			error = 1;
		} 
		
		if (error != 1) {		
			var gender;
			if ($('memberGenderMale'+tabID).checked == true) {
				gender = 'Male';
			} else if ($('memberGenderFemale'+tabID).checked == true) {
				gender = 'Female';
			}
			
			/*
			 * Get school type from radio buttons
			 */
			var schoolType;		
			var allSchoolTypeRadio  = document.getElementsByClassName('radioSchoolType'+tabID);
			for(i=0;i<allSchoolTypeRadio.length;i++) {
				if (allSchoolTypeRadio[i].checked == true) {
					schoolType = allSchoolTypeRadio[i].value;					
				}			
			}	
		
			/*
			 * Get member type from checkboxes
			 */
			var memberTypeString = '';		
			var allMemberTypeCheckbox  = document.getElementsByClassName('checkboxMemberType'+tabID);
			for(i=0;i<allMemberTypeCheckbox.length;i++) {
				if (allMemberTypeCheckbox[i].checked == true) {
					memberTypeString += allMemberTypeCheckbox[i].value+'|';					
				} else {
					memberTypeString += '|';
				}
			}		
		
			var memberRoleString = '';		
			var allMemberRoleSelect  = document.getElementsByClassName('selectRole'+tabID);
			for(i=0;i<allMemberRoleSelect.length;i++) {
				if ( ($F(allMemberRoleSelect[i]) != 'Select...') && ($F(allMemberRoleSelect[i]) != '') ) {
					memberRoleString += $F(allMemberRoleSelect[i])+'|';					
				} else {
					memberRoleString += '|';
				}		
			}		
			
			if ($('familyLowLiteracy'+tabID).checked == true) {
				var familyLowLiteracy = 1;
			}
			if ($('familyLowIncome'+tabID).checked == true) {
				var familyLowIncome = 1;
			}
			
			var dataString = {
				tabID:                tabID,
				memberID:             $F('memberID'+tabID),
				familyID:             $F('familyID'),
				nameFirst:            $F('nameFirst'+tabID),
				nameLast:             $F('nameLast'+tabID),
				address:              $F('address'+tabID),
				city:                 $F('city'+tabID),
				state:                $F('state'+tabID),
				zip:                  $F('zipCode'+tabID),
				county:               $F('selectCounty'+tabID),
				phone1:               $F('phone1'+tabID),
				phone2:               $F('phone2'+tabID),
				phone3:               $F('phone3'+tabID),
				phone4:               $F('phone4'+tabID),
				email:                $F('email1'+tabID),
				email2:               $F('email2'+tabID),
				language:             $F('selectLanguage'+tabID),
				ethnicity:            $F('selectEthnicity'+tabID),
				school:               $F('nameOfSchool'+tabID),
				schoolType:           schoolType,
				district:             $F('selectSchoolDistrict'+tabID),				
				gender:               gender,
				memberType:           memberTypeString,
				memberRole:           memberRoleString,				
				childAgeYr:           $F('childAgeYr'+tabID),
				childAgeMo:           $F('childAgeMonth'+tabID),
				childDOB:             $F('dateFieldBirth'+tabID),
				profOrg:              $F('professionalOrg'+tabID),
				profURL:              $F('professionalURL'+tabID),	
				profTitle:            $F('professionalTitle'+tabID),	
				familyLowLiteracy:    familyLowLiteracy,
				familyLowIncome:      familyLowIncome,
				parentSingle:         $F('parentSingle'+tabID),
				okToContactPhoneHome: $F('okToContactPhoneHome'+tabID),
				okToContactPhoneWork: $F('okToContactPhoneWork'+tabID),
				okToContactEmail:     $F('okToContactEmail'+tabID)
			};	
			
			jsonString = Object.toJSON(dataString);
			
			if ($F('memberID'+tabID) == 0) {
				// Turn on buttonDeleteMember
				$('buttonDeleteMember'+tabID).style.display = '';
			}
			
			var params = 'handler=saveMember&jsonString='+jsonString+'&contactID='+contactID;
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport) {
					contactSaveFamilyTeamObj.returnSaveTeamMember(transport.responseText);
				},
				onFailure: function() {
					alert('Oops!');
				}	
			});
			
		}	
	},
	returnSaveTeamMember : function(response) {
		responseArray = response.split('|');
		var tabID    = responseArray[0];
		var memberID = responseArray[1];
		var familyID = responseArray[2];
		var nameFull = responseArray[3];
		
		$('memberID'+tabID).value = memberID;
		$('activeMemberID').value = memberID;
		$('familyID').value = familyID;
				
		updateStatus('Member updated!','success','messageMember'+tabID);
		/*
		 * Update clientName id
		 */
		if (nameFull) {
			$('clientName').innerHTML = '('+nameFull+')';
		}	
	},
	deleteTeamMember : function(tabID) {
		var memberID = $F('memberID'+tabID);
		var params = 'handler=deleteMember&memberID='+memberID;
			
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params
		});
		
		/*
		 * Remove tab from tabstrip
		 */
		
		var activeTab = tabs.activeTab.getId();
		var nextActiveTab = activeTab+1;
		var prevActiveTab = activeTab-1;
		tabs.hideTabStripItem(activeTab);
		
		if (nextActiveTab) {
			tabs.setActiveTab(nextActiveTab);	
		} else if (prevActiveTab) {
			tabs.setActiveTab(prevActiveTab);	
		}
	},
	removeTeamMemberFromContact : function(tabID) {
		var contactID = $F('contactID');
		var memberID  = $F('memberID'+tabID);
		var params = 'handler=removeTeamMemberFromContact&contactID='+contactID+'&memberID='+memberID;
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params			
		});	
	}
}

var contactSaveClientInfoObj = {
	saveClientInfoSocial : function(contactID) {
		var contactID = $F('contactID');
		var memberID  = $F('activeMemberID');
		
		var dataString = {
				socialConcerns: $F('socialConcerns'),
				socialResponse: $F('socialResponse')
		};		
		jsonString = Object.toJSON(dataString);
		
		var params = 'handler=saveClientInfoSocial&jsonString='+jsonString+'&memberID='+memberID;
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,	
			onSuccess: function(transport) {
				updateStatus('Client information updated!','success','messageClientInformation');
				contactFormGetInfoObj.getClientInfoSocial(memberID);
			},
			onFailure: function() {
				alert('Oops!');
			}		
		});	
	},
	saveClientInfoSchoolTrans : function(contactID) {
		var contactID = $F('contactID');
		var memberID  = $F('activeMemberID');
		
		var dataString = {
				iepClass1:       $F('schoolIEPClassification1'),
				iepClass2:       $F('schoolIEPClassification2'),
				spedDesignation: $F('schoolSPEDDesignation'),
				diplomaType:     $F('schoolDiplomaType'),			
				schoolConcerns:  $F('schoolConcerns'),
				schoolResponse:  $F('schoolResponse')
		};		
		jsonString = Object.toJSON(dataString);
		
		var params = 'handler=saveClientInfoSchoolTrans&jsonString='+jsonString+'&memberID='+memberID;
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,	
			onSuccess: function(transport) {
				updateStatus('Client information updated!','success','messageClientInformation');
				contactFormGetInfoObj.getClientInfoSchoolTrans(memberID);
			},
			onFailure: function() {
				alert('Oops!');
			}		
		});	
	},
	saveClientInfoFamily : function(contactID) {
		var contactID = $F('contactID');
		var memberID  = $F('activeMemberID');
		
		var dataString = {
				lowIncome:       $F('familyLowIncome'),
				familyConcerns:  $F('familyConcerns'),
				familyResponse:  $F('familyResponse')
		};		
		jsonString = Object.toJSON(dataString);
		
		var params = 'handler=saveClientInfoFamily&jsonString='+jsonString+'&memberID='+memberID;
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,	
			onSuccess: function(transport) {
				updateStatus('Client information updated!','success','messageClientInformation');
				contactFormGetInfoObj.getClientInfoFamily(memberID);
			},
			onFailure: function() {
				alert('Oops!');
			}		
		});	
	},
	saveClientInfoEvents : function(contactID) {
		
	},
	saveClientInfoLibrary : function(contactID) {
		
	},
	toggleClientInfoEventAttended : function(regID) {
		var params = 'handler=toggleEventAttended&regID='+regID;
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,	
			onSuccess: function(transport) {
				var response = transport.responseText;
				var responseArray = response.split('|');
				var regID = responseArray[0];
				var state   = responseArray[1];
				
				if (state == 1) {
					$('attendIcon'+regID).className = 'statusClosed';
				} else {
					$('attendIcon'+regID).className = 'statusAlert';
				}
			}	
		});	
	},
	saveClientInfoPartners : function(contactID) {
		
	},
	saveClientInfoSubscriptions : function(contactID) {
		
	}
}

var contactStatusUpdateObj = {
	contactCloseFromRow : function(contactID,origCssClass) {
		var params = 'handler=contactStatusUpdate&contactID='+contactID+'&newStatus=close';
	
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
			method: 'POST',
			parameters: params,	
			onSuccess: function(transport) {
				fadeRowBG('green','contactRow'+contactID,7,origCssClass);
				/*
				 * Replace status icon
				 */
				$('statusCell'+contactID).innerHTML = '<div ext:qtip="Closed" class="statusClosed"></div>';
			}		
		});	
	},
	contactCloseFromContactForm : function() {
		
	},
	contactReopen : function(contactID) {
		
	}
}

var contactSaveNotesObj = {
	saveNotes : function() {
		var contactID = $F('contactID');
		if (!contactID || (contactID == 0)) {
			updateStatus('Please enter a subject for this contact!', 'error', 'messageNotes');
		} else {
			var dataString = {
				notes: $F('contactNotes')
			};
			jsonString = Object.toJSON(dataString);
			
			var params = 'handler=saveMessage&jsonString=' + jsonString + '&itemID=' + contactID + '&type=contact';
			
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,
				onSuccess: function(transport){
					$('contactNotes').value = '';
					getNoteDataObj.renderNotes(transport.responseText);
					updateStatus('Contact notes have been updated!', 'success', 'messageNotes');
				},
				onFailure: function(){
					alert('Oops!');
				}
			});
		}	
	}
}
